package Listeners;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUI.AuthorToolPanel;
import GUI.ConferenceWizardFrame;
import GUI.SubChairToolPanel;
import Model.Conference;
import Model.Conference.Role;
import Model.Paper;
import Model.User;

/**
 * 
 * @author William Kim
 * 
 *         This Listener controls the functionality to assign reviewers to
 *         papers. This code is based on code written by teammates in
 *         AccessConferenceListener.
 */
public class AssignReviewerListener extends AbstractAction
{
  final private Conference myConference;
  final private Paper myPaper;
  final private User myReviewer;
  final private ConferenceWizardFrame myFrame;
  final private User mySPC;
  final private Map<String, User> userMap = new HashMap<String, User>();

  public AssignReviewerListener(final Conference the_conference,
      final Paper the_paper, final User the_user,
      final ConferenceWizardFrame the_frame, final User the_SPC)
  {
    myConference = the_conference;
    myPaper = the_paper;
    myReviewer = the_user;
    myFrame = the_frame;
    mySPC = the_SPC;
  }

  @Override
  public void actionPerformed(final ActionEvent arg0)
  {
    final List<String> currentReviewers = new ArrayList<String>();
    for (User u : myConference.users())
    {
      if (myConference.getRoles(u).contains(Role.REVIEWER)
          && !myPaper.getMyReviewAssociation().keySet().contains(u) // is not
                                                                    // already
                                                                    // reviewer
          && !myPaper.getAuthor().equals(u) // is not author
          && myConference.getPapersAssignedToReviewers(u).size() < 4)
      {
        currentReviewers.add(u.realName());
        userMap.put(u.realName(), u);
      }
    }

    final String reviewer = (String) JOptionPane.showInputDialog(null,
        "Select a user to become reviewer.", "Assign Reviewer",
        JOptionPane.PLAIN_MESSAGE, null, currentReviewers.toArray(), null);

    myPaper.editReview(userMap.get(reviewer), null);

    myFrame
        .setCurrentPanel(new SubChairToolPanel(myConference, mySPC, myFrame));

  }

}
